#include "Thread.h"	
#include <iostream>
#include <unistd.h>	

/*Constructor*/	
Thread::Thread(){
		isrun=true;
		seguirCorriendo=true;
}	

/*Lanza el thread*/
void Thread::run(){
		isrun=true;
		seguirCorriendo=true;
		if(pthread_create(&thread,NULL,static_run,this)!=0)
			printf("EROOR CREATE\n");
}


/* Finaliza el thread*/
void Thread::stop(){
		seguirCorriendo=false;
}

bool Thread::isAlive() const{ return isrun;}

/* Espera a que el thread termine */
void Thread::join(){		
		if(pthread_join(thread,NULL)!=0)
  		      std::cout << "Error join" <<std::endl;
	   	isrun=false;
		seguirCorriendo=false;
}
/*Destructor*/
Thread::~Thread(){		
		if(isrun==true){
		   pthread_join(thread,NULL);  					
		   isrun=false;
		   seguirCorriendo=false;
	}
	pthread_exit(NULL);
}

void Thread::sleep(unsigned long msecs){
		::sleep(msecs);
	}

void *Thread::static_run(void* arg){
		((Thread*) arg)-> main();
		return NULL;
}
